package org.crazyit.activiti_d2._6_SortProblem;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.impl.AbstractQuery;
import org.activiti.engine.impl.GroupQueryProperty;

import java.util.List;

import static org.crazyit.activiti_d2._2_ListData.ListData.createGroup;

public class SortProblem {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份认证服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 写入2条新用户组数据
        //createGroup(identityService,"12","GroupA","typeA");
        //createGroup(identityService,"13","GroupB","typeB");

        //4. 根据ID 升序排序
        //List<Group> list1 = identityService.createGroupQuery().orderByGroupId().asc().list();
        //4.1 解决方案1<不推荐>: 使用AbstractQuery 抽象类的orderBy() 方法
        AbstractQuery abstractQuery = (AbstractQuery) identityService.createGroupQuery();
        List<Group> list1 = abstractQuery.orderBy(new GroupQueryProperty("RES.ID_ + 0")).asc().list();

        //4.2 解决方案2<推荐>: 使用原生SQL
        List<Group> list2 = identityService.createNativeGroupQuery().sql("SELECT * FROM ACT_ID_GROUP ORDER BY ID_ + 0 ASC").list();

        //5. 遍历输出查询结果集
        for (Group data : list1) {
            System.out.println("   " + data.getId() + "---" + data.getName());
        }

        for (Group data : list2) {
            System.out.println("   " + data.getId() + "---" + data.getName());
        }

    }

}
